package com.neu.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.api.dto.FlavorDTO;
import com.neu.api.mapper.FlavorMapper;
import com.neu.api.pojo.Flavor;
import com.neu.api.query.FlavorQuery;
import com.neu.api.service.FlavorService;
import com.neu.api.utils.PageResult;
import com.neu.api.vo.FlavorVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FlavorServiceImpl implements FlavorService {

    @Resource
    FlavorMapper flavorMapper;

    @Override
    public PageResult<FlavorVO> find(FlavorQuery flavorQuery) {
        //设置是否需要分页
        PageHelper.startPage(flavorQuery.getPage(),flavorQuery.getPageSize());  //告诉PageHelper此处需要分页

        com.github.pagehelper.Page<FlavorVO> page = (com.github.pagehelper.Page<FlavorVO>) flavorMapper.find(flavorQuery);

        PageInfo<FlavorVO> pageInfo = page.toPageInfo();


        List<FlavorVO> flavorVOList = pageInfo.getList();
        long total = pageInfo.getTotal();

        PageResult<FlavorVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(flavorVOList);

        return pageResult;
    }

    @Override
    public void add(FlavorDTO flavorDTO) {
        Flavor flavor = new Flavor();
        BeanUtils.copyProperties(flavorDTO,flavor);
        flavorMapper.add(flavor);
    }


    @Override
    public void update(long id, FlavorDTO flavorDTO) {
        Flavor flavor = new Flavor();
        BeanUtils.copyProperties(flavorDTO,flavor);
        flavor.setId(id);
        flavorMapper.update(flavor);
    }

    @Override
    public void deleteById(long id) {
        flavorMapper.deleteById(id);
    }
}
